package com.xidian.zhiancloud.controller.chemicalsManagement.wastePart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.WasteStorage;
import com.xidian.zhiancloud.qo.chemicalsManagement.WasteStorageQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart.WasteStorageService;
import com.xidian.zhiancloud.vo.chemicalsManagement.WasteStorageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * 危化品管理 废弃物管理 废弃物入库
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/WasteStorage")
public class WasteStorageController extends BaseController {

    @Autowired
    private WasteStorageService wasteStorageService;

    /**
     * 增加废弃物入库记录
     *
     * @param wasteStorage 废弃物入库对象
     * @return 返回是否添加成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/add")
    public ResultDTO<Boolean> add(WasteStorage wasteStorage) {
        wasteStorage.setStorageTime(new Date(System.currentTimeMillis()));
        wasteStorage.setStoragePersonId(getSessionUserid());
        wasteStorage.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(wasteStorageService.save(wasteStorage), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查找废弃物入库记录
     *
     * @param id 废弃物入库id
     * @return 返回废弃物入库记录
     */
    @GetMapping("/getById")
    public ResultDTO<WasteStorageVO> getById(Long id) {
        return ResultDTO.successOf(wasteStorageService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id进行入库提交
     * @param id 废弃物入库id
     * @return 返回是否入库成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/submitById")
    public ResultDTO<Boolean> submitById(Long id) {
        return ResultDTO.successOf(wasteStorageService.submitById(id), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 列表查询
     *
     * @param wasteStorageQO 请求参数
     * @return 返回废弃物入库列表
     */
    @GetMapping("/getListByQueryObject")
    public ResultDTO<IPage<WasteStorageVO>> getListByQueryObject(WasteStorageQO wasteStorageQO){
        if (wasteStorageQO.getCurrent() == null || wasteStorageQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        wasteStorageQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(wasteStorageService.getListByQueryObject(wasteStorageQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param wasteStorageQO 请求参数
     * @return 返回废弃物入库台账
     */
    @GetMapping("/getAccountListByQueryObject")
    public ResultDTO<IPage<WasteStorageVO>> getAccountListByQueryObject(WasteStorageQO wasteStorageQO){
        if (wasteStorageQO.getCurrent() == null || wasteStorageQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        wasteStorageQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(wasteStorageService.getAccountListByQueryObject(wasteStorageQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        ResultDTO.successOf(wasteStorageService.export(response, ids), GeneralResponseEnums.SUCCESS);
    }
}
